﻿using CoreShop.PrintService;
using Sunny.UI;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIMenuItemPay : UIUserControl
    {
        public UPaymentnfo Info { get; }

        public UIMenuItemPay()
        {
            InitializeComponent();

            Info = new UPaymentnfo();
            Text = Info.Name;
        }

        public UIMenuItemPay(UPaymentnfo info)
        {
            InitializeComponent();
            Info = info;
            Text = info.Name;
            ForeColor = info.ForeColor;
        }

        private bool selected = false;
        public bool Selected
        {
            get => selected;
            set
            {
                selected = value;
                Invalidate();
            }
        }

        private Color selectedColor = Color.FromArgb(221, 221, 221);
        public Color SelectedColor
        {
            get => selectedColor;
            set
            {
                selectedColor = value;
                Invalidate();
            }
        }

        private int itemHeight = 42;

        public int ItemHeight
        {
            get => itemHeight;
            set
            {
                itemHeight = value;
                Invalidate();
            }
        }

        public void SetMinSize(int width = 340)
        {
            ItemHeight = 25;//30
            Width = width;
            Font = new Font("微软雅黑", 12f);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (Selected)
            {
                e.Graphics.FillRectangle(SelectedColor, 0, 0, Width, Height);
            }

            if (Info == null) return;

            e.Graphics.DrawString(Text, Font, ForeColor, new Rectangle(26 , 0, Width, ItemHeight), ContentAlignment.MiddleLeft);

            if (Info.Amount.IsValid())
                e.Graphics.DrawString("￥" + Info.Amount, Font, ForeColor, new Rectangle(Width / 3 + 10, 2, Width / 2, 30), ContentAlignment.MiddleLeft);

            if (Info.Price.IsValid() && Info.IsReturning)
            {
                using (Font font = new Font("微软雅黑", 10.5f))
                    e.Graphics.DrawString("退", font, Color.Red, new Rectangle(Width / 3 + 115, 1, Width / 3, 30), ContentAlignment.MiddleLeft);

                e.Graphics.DrawRoundRectangle(Color.FromArgb(255, 186, 33), new Rectangle(Width / 3 + 115, 6, 22, 22), 5);

                e.Graphics.DrawString(Info.Price, Font, Color.Red, new Rectangle(Width / 3 + 140, 1, Width / 3, 30), ContentAlignment.MiddleLeft);
            }
            else
            if (Info.abnormal.IsValid() && Info.IsAbnormal)
            {
                using (Font font = new Font("微软雅黑", 10.5f))
                    e.Graphics.DrawString("异常", font, Color.Red, new Rectangle(Width / 3 + 115, 1, Width / 3, 30), ContentAlignment.MiddleLeft);

                e.Graphics.DrawRoundRectangle(Color.FromArgb(255, 186, 33), new Rectangle(Width / 3 + 115, 6, 38, 22), 5);

                e.Graphics.DrawString(Info.abnormal, Font, Color.Red, new Rectangle(Width / 3 + 155, 1, Width / 3, 30), ContentAlignment.MiddleLeft);
            }

            int top = ItemHeight;            

            if (Height != top) Height = top;
        }

        private void UIMenuItem_Click(object sender, System.EventArgs e)
        {
            Selected = true;
            foreach (var item in Parent.GetControls<UIMenuItem>())
            {
                if (item.Equals(this)) continue;

                item.Selected = false;
            }
        }
    }
}
